package com.zj.httpdemo.http.get;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.zj.httpdemo.R;

import java.io.File;

public class HttpActivity extends AppCompatActivity {
    private static final String TAG = HttpActivity.class.getSimpleName();

    private ImageView imageView;
    private NumberProgressBar numberProgressBar;

    private boolean isFirst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);

//        imageView = (ImageView) findViewById(R.id.image);
        numberProgressBar = (NumberProgressBar) findViewById(R.id.progress);

//        new StringTask().execute("https://raw.githubusercontent.com/zjZSTU/Resources/master/Get/test.txt");
//        new JsonTask().execute("https://raw.githubusercontent.com/zjZSTU/Resources/master/Get/test.json");
//        new BmpTask().execute("https://raw.githubusercontent.com/zjZSTU/Resources/master/Get/img.jpg");

        String downloadPath = "https://github.com/zjZSTU/Resources/blob/master/Get/icon.zip?raw=true";
        String savePath = getExternalFilesDir(null).getAbsolutePath() + File.separator + "icon.zip";
        String[] params = {downloadPath, savePath};

        new DataTask().execute(params);
    }

    private class BmpTask extends BitmapTask {
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            imageView.setImageBitmap(bitmap);
        }
    }

    private class DataTask extends BigDataTask {
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            Log.e(TAG, "onProgressUpdate: loaded = " + values[0] + " max = " + values[1]);

            if (isFirst) {
                isFirst = false;
                numberProgressBar.setMax(100);
                numberProgressBar.setProgress(0);
            }

            numberProgressBar.setProgress(values[0] * 100 / values[1]);
        }
    }
}
